package com.cxy.bio;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @ClassName: ServerPool
 * @Description: TODO(BIO通信的服务端 线程池)
 * @author 菜徐鸭
 * @date 2020年5月9日
 *
 */
public class ServerPool {

	private static ExecutorService executorService = Executors
			.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

	public static void main(String[] args) throws IOException {
		// 服务端启动必备
		ServerSocket serverSocket = new ServerSocket();
		// 表示服务端在哪个端口上监听
		serverSocket.bind(new InetSocketAddress(10001));
		System.out.println("服务启动...");
		try {
			while (true) {
				executorService.execute(new ServerTask(serverSocket.accept()));
			}
		} catch (Exception e) {
			serverSocket.close();
		}
	}

	// 每个和客户端的通信都会打包成一个任务，交一个线程来执行
	private static class ServerTask implements Runnable {

		private Socket socket = null;

		public ServerTask(Socket socket) {
			this.socket = socket;

		}

		@Override
		public void run() {
			// 实例化与客户端通信的输入输出流
			try (ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
					ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream())) {
				// 接收客户端的输出，也就是服务器的输入
				String userName = inputStream.readUTF();
				System.out.println("接收到客户端消息：" + userName);

				// 服务器的输入，也就是客户端的输入
				outputStream.writeUTF("Hello," + userName);
				outputStream.flush();

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					socket.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

	}
}